package com.yc.hr.util;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Window;

import javax.swing.ImageIcon;
import javax.swing.JComponent;

public class UIUtils {
	/**
	 * 将图片绘制到指定的控件上, 图片拉伸至控件大小
	 * @param g  		 图形对象
	 * @param component  要绘制的控件
	 * @param imagePath  图片的文件路径
	 */
	public static void paintComponent(Graphics g, JComponent component, String imagePath) {
        // 加载图片
        ImageIcon icon = new ImageIcon(imagePath);
        Image image = icon.getImage();
        // 绘制图片，使用 getWidth() 和 getHeight() 获取组件大小
        g.drawImage(image, 0, 0, component.getWidth(), component.getHeight(), component);
    }
	
	/**
	 * 设置窗口居中
	 * @param window
	 */
	public static void setCenter(Window window) {
        Dimension screenSize = GraphicsEnvironment.getLocalGraphicsEnvironment()
        		.getMaximumWindowBounds().getSize();
        
        // 计算窗口居中的位置
        int centerX = (screenSize.width - window.getWidth()) / 2;
        int centerY = (screenSize.height - window.getHeight()) / 2;
        
        // 设置窗口的位置
        window.setLocation(centerX, centerY);
	}
}
